// pages/detail/detail.js
const app = getApp();
Page({
  data: {
    ac_name: '活动名称',
    ac_start_time: '开始时间',
    ac_end_time: '结束时间',
    ac_num: '参加人数',
    goodsName: '商品名称',
    activity: '',
    activity_attr: '',
    id: "",
    editStatus: ""
  },

  // 编辑
  edit: function() {
    let that = this;
    if (that.data.editStatus == 1) {
      wx.showModal({
        title: '提示',
        content: '活动暂停后才可编辑',
        confirmText: "暂停",
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '正在请求操作...',
            })
            wx.request({
              url: app.buildUrl('/shop/active/end'),
              header: app.buildHeader(),
              data: {
                id: that.data.id
              },
              method: "POST",
              success(res) {
                wx.hideLoading();
                if (res.data.code == 200) {
                  app.showToast(res.data.msg)
                  setTimeout(() => {
                    wx.navigateTo({
                      url: '../update/update?id=' + that.data.id
                    })
                  }, 1000)
                } else {
                  app.showToast(res.data.msg)
                }
              }
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '../update/update?id=' + that.data.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id,
      editStatus: options.status
    })
  },
  getDate(date) {
    let Y = new Date(date * 1000).getFullYear();
    let M = new Date(date * 1000).getMonth() + 1;
    let D = new Date(date * 1000).getDate();
    M = M < 10 ? "0" + M : M;
    D = D < 10 ? "0" + D : D;
    return `${Y}-${M}-${D}`
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.buildUrl('/shop/active/select'),
      method: 'POST',
      header: app.buildHeader(),
      data: {
        id: that.data.id
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          let activity = res.data.data.activity;
          activity.begin_time = that.getDate(activity.begin_time);
          activity.end_time = that.getDate(activity.end_time);
          that.setData({
            activity: activity,
            activity_attr: res.data.data.activity_attr
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})